<div id='divFormCrearColmena'></div>
<br/>
<table id='tablaListarColmena' class='display' cellpadding="0" cellspacing="0" border="0">
	<thead>
		<tr>
			<th width="50px">ID</th>
			<th >Nombre</th>
			<th width="50px">Estado</th>
			<th width="50px">Acción</th>
		</tr>
	</thead>
	<tbody>
	</tbody>
</table>
<script>
var oTable;
var aListaColmenas = <?=$aListaColmenas?>;
var iStart=<?=$startPoint?>;
iStart=parseInt(iStart)-5;
iStart=(iStart<0)?0:iStart;
$(document).ready(function(){

	oTable = $('#tablaListarColmena').dataTable({
		"bJQueryUI": false,
		"bStateSave": false,
		"aaData": aListaColmenas,
		"sPaginationType": "full_numbers",
		"iDisplayStart": iStart ,
		"iDisplayLength": 5,		
		"aoColumnDefs": [
			{
				"fnRender": function ( oObj ) {return oObj.aData[0];},
			    "sClass": "center",
			    "aTargets": [ 0 ],
			    "bUseRendered": false
			},
			{
				"fnRender": function ( oObj ) {return oObj.aData[1];},
			    "sClass": "left",
			    "aTargets": [ 1 ],
			    "bUseRendered": false
			},
			{
				"fnRender": function ( oObj ) {
				var strImg="<img src='"+oObj.aData[3]+"' />";
				return strImg;
				},
			    "sClass": "center",
			    "aTargets": [ 2 ],
			    "bUseRendered": false
			},
			{
		    	"fnRender": function ( oObj ) {
		    	var idColmena = oObj.aData[0];
				var strOpen = '<div class="iconsAction">';
					var strEdit = '<img class="pointer" title="Editar Colmena" src="../public/imag/icons/pencil.png" onclick="editData('+idColmena+','+oObj.iDataRow+')">';
					var strDelete = '<img class="pointer" title="Eliminar Colmena" src="../public/imag/icons/delete.png" onclick="deleteData('+idColmena+','+oObj.iDataRow+')">';
					var strClose = '</div>';
			     	return strOpen + strEdit + strDelete + strClose;		     		
		     	},
		     	"bSortable": false,
                "sClass": "center", 
		     	"aTargets": [ 3 ],
		     	"bUseRendered": false
		    }

		]
	});


	
	$("#divFormCrearColmena").formation({isAjax: true,
		id: 'formCrearColmena'
	});
	$.formation.addTitle({labelValue: "Administrar Colmenas"});
	$.formation.addInput({
		type:			'text', 
		name:			'<?=Colmena::COL_ID?>',
		id:				'<?=Colmena::COL_ID?>',		
		labelValue: 	'ID:',
		required: 		false
	});
	$('#<?=Colmena::COL_ID?>').attr('readonly', 'readonly');
	$.formation.addInput({
		type:			'text', 
		name:			'<?=Colmena::COL_NOMBRE?>',
		id:				'<?=Colmena::COL_NOMBRE?>',		
		labelValue: 	'Nombre:',
		required: 		true
	});	
	$.formation.addRadios({"1":"Si","0":"No"},{
		name:'<?= Colmena::COL_ESTADO ?>',
		id: '<?= Colmena::COL_ESTADO ?>',
		legend:'¿ Activo ?',
		labelValue:'Estado del Registro',
		defaultValue: '1',
		required: true
	});	
	$.formation.addButton({value: "Guardar", id:'submitColmena'});		

	$('#formCrearColmena').submit(function() {
		if($.formation.getErrors()){
			var strPage = "procesador.php?controller=colmena&action=salvar";
			var dataFormulario = $('#formCrearColmena').serializeArray();
			$.ajax({
				  async: false,				  
				  type: "POST",
				  url: strPage,
				  dataType: 'json',
				  data: {'dataFormulario': dataFormulario},
				  cache: false,
				  success: function(result){
						if(result.success > 0){
							$('#mainPanel').load('procesador.php?controller=colmena&action=crear',{'startPoint':result.success});
						}
						else{
							jAlert('ERROR');
						}
				  }
			});
		}
	});	
	//sobrecarga limpiar general
	$(':reset').click(function(){
		$('#<?=Colmena::COL_ID?>').attr("value","");
		$('#<?=Colmena::COL_NOMBRE?>').attr("value","");
		$('input[name=<?=Colmena::COL_ESTADO?>]:checked').removeAttr('checked');
		$('input[name=<?=Colmena::COL_ESTADO?>]:eq(0)').attr('checked','checked');
	});
	
});

function editData(idColmena,iNivel)
{
	$('#<?=Colmena::COL_ID?>').attr("value",aListaColmenas[iNivel][0]);
	$('#<?=Colmena::COL_NOMBRE?>').attr("value",aListaColmenas[iNivel][1]);
	$('input[name=<?=Colmena::COL_ESTADO?>]:checked').removeAttr('checked');
	var iPosIlogica=(aListaColmenas[iNivel][2]==1)?0:1;
	$('input[name=<?=Colmena::COL_ESTADO?>]:eq('+iPosIlogica+')').attr('checked','checked');
}


function deleteData(idColmena,iNivel)
{
	if(confirm("Esta Seguro/a"))
	{
		var strPage = "procesador.php?controller=colmena&action=eliminar";
		$.ajax({
			  async: false,				  
			  type: "POST",
			  url: strPage,
			  dataType: 'json',
			  data: {'<?=Colmena::COL_ID?>': idColmena},
			  cache: false,
			  success: function(result){
					if(result.success > 0){
						$('#mainPanel').load('procesador.php?controller=colmena&action=crear',{'startPoint':iNivel});
					}
					else{
						jAlert('ERROR');
					}
			  }
		});
	}
}


</script>